//
//  KCLoginViewController.swift
//  S0002_NavigationController
//
//  Created by yao_yu on 14-9-1.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import UIKit

class KCLoginViewController: UIViewController {

    var delegate:KCMainDelegate!
    var userName:UITextField = UITextField()
    var password:UITextField = UITextField()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        addLoginForm()
    }
    
    func addLoginForm(){
        let mcolor = UIColor(red: 51, green: 133, blue: 255, alpha: 0)
        let lblUserName = UILabel(frame: CGRectMake(50, 150, 100, 30))
        lblUserName.text = "用户名"
        view.addSubview(lblUserName)
        
        userName.frame = CGRectMake(120, 150, 150, 30)
        userName.borderStyle = UITextBorderStyle.RoundedRect
        view.addSubview(userName)
        
        let lblPWD = UILabel(frame: CGRectMake(50, 200, 100, 30))
        lblPWD.text = "密码"
        view.addSubview(lblPWD)
        
        password.frame = CGRectMake(120, 200, 150, 30)
        password.secureTextEntry = true
        password.borderStyle = UITextBorderStyle.RoundedRect
        view.addSubview(password)
        
        let btnLogin = UIButton.buttonWithType(UIButtonType.System) as UIButton
        btnLogin.frame = CGRectMake(70, 270, 80, 30)
        btnLogin.setTitle("登录", forState: UIControlState.Normal)
        btnLogin.backgroundColor = mcolor
        view.addSubview(btnLogin)
        btnLogin.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        let btnCancel = UIButton.buttonWithType(UIButtonType.System) as UIButton
        btnCancel.frame = CGRectMake(170, 270, 80, 30)
        btnCancel.setTitle("取消", forState: UIControlState.Normal)
        btnCancel.backgroundColor = mcolor
        view.addSubview(btnCancel)
        btnCancel.addTarget(self, action: "cancel", forControlEvents: UIControlEvents.TouchUpInside)
}
    
    func login(){
        if userName.text == "yy" && password.text == "1" {
            self.delegate.showUserInfoWithUserName(userName.text)
            self.dismissViewControllerAnimated(true, completion: nil)
        }
        else {
            let alert = UIAlertView(title: "系统消息", message: "用户名密码错误, 请重新输入", delegate: nil, cancelButtonTitle: "确定")
            alert.show()
        }
            
    }
    
    func cancel(){
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
